import { login, logout, getInfo } from '@/api/user'
import { encrypt } from '@/utils/encode.js'
import { MessageBox, Message } from 'element-ui'
import { getToken, setToken, removeToken, getRoles, setRoles, removeRoles } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    id: getRoles(),
    name: '',
    username: '',
    password: '',
    department: '',
    department_name: '',
    circulation_user: '',
    avatar: '',
    first_name:'',
    roles: [],
    screenfull_name: ''
  }
}
const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_ID: (state, id) => {
    state.id = id
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_USERNAME: (state, name) => {
    state.username = name
  },
  SET_PASSWORD: (state, pass) => {
    state.password = pass
  },
  SET_DEPARTMENT: (state, department) => {
    state.department = department
  },
  SET_DEPARTMENT_NAME: (state, department_name) => {
    state.department_name = department_name
  },
  SET_CIRCULATIONUSER: (state, user) => {
    state.circulation_user = user
  },
  SET_FIRSTNAME: (state ,first_name) =>{
    state.first_name = first_name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_SCREENFULL_NAME: (state, screenfull_name) => {
    state.screenfull_name = screenfull_name
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      // console.log('secx:' + encrypt(username.trim()))
      // commit('SET_USERNAME', encrypt(username.trim()))
      // commit('SET_PASSWORD', encrypt(password))
      login({ username: encrypt(username.trim()), password: encrypt(password) }).then(response => {
        if (response.data) {
          const { token, userinfo } = response.data
          commit('SET_TOKEN', token)
          commit('SET_ID', userinfo.id)
          setRoles(userinfo.id)
          setToken(token)
          resolve()
        } else {
          reject('用户名、工号或者密码错误')
        }
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.id).then(response => {
        const { data } = response
        if (!data) {
          reject('Verification failed, please Login again.')
        }

        const { role, name, username, department, department_name, circulation_user,first_name, screenfull_name } = data

        // roles must be a non-empty array
        if (!role || role.length <= 0) {
          reject('getInfo: roles must be a non-null array!')
        }
        commit('SET_DEPARTMENT', department)
        commit('SET_DEPARTMENT_NAME', department_name)
        commit('SET_CIRCULATIONUSER', circulation_user)
        commit('SET_FIRSTNAME',first_name)
        commit('SET_ROLES', role)
        commit('SET_NAME', name)
        commit('SET_USERNAME', username)
        commit('SET_SCREENFULL_NAME', screenfull_name)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
      // const user = JSON.parse(getRoles())

      // const { name, roles } = user

      // // roles must be a non-empty array
      // if (!roles || roles.length <= 0) {
      //   reject('getInfo: roles must be a non-null array!')
      // }

      // commit('SET_ROLES', roles)
      // commit('SET_NAME', name)
      // getInfo(1).then(response => {
      //   const { data } = response
      //   console.log(data)
      //   if (!data) {
      //     reject('Verification failed, please Login again.')
      //   }

      //   const { groups, username } = data

      //   // roles must be a non-empty array
      //   if (!groups || groups.length <= 0) {
      //     reject('getInfo: roles must be a non-null array!')
      //   }
      //   console.log(typeof groups + username)
      // })
      // //   commit('SET_ROLES', roles)
      // //   commit('SET_NAME', name)
      // //   commit('SET_AVATAR', avatar)
      // resolve(user)
      // // }).catch(error => {
      // //   reject(error)
      // // })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        removeRoles() // remove roles
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

